Hands-on-exercise4

4  Programming Animated Statistical Graphics with R

4.2 Data Preparation

pacman::p_load(readxl, gifski, gapminder,
               plotly, gganimate, tidyverse)
col <- c("Country", "Continent")
globalPop <- read_xls("data/GlobalPopulation.xls",
                      sheet="Data") %>%
  mutate_at(col, as.factor) %>%
  mutate(Year = as.integer(Year))

4.3 Animated Data Visualisation: gganimate methods

I tried use theme_minimal() and cubic-in-out ,shadow_wake toSmooth out animations and ghosting

ggplot(globalPop, aes(x = Old, y = Young, 
                      size = Population, 
                      colour = Country)) +
  geom_point(alpha = 0.7, 
             show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  labs(title = 'Year: {frame_time}', 
       x = '% Aged', 
       y = '% Young') +
  transition_time(Year) +       
  ease_aes('linear')  
ggplot(globalPop, aes(x = Old, y = Young, 
                      size = Population, 
                      colour = Country)) +
  geom_point(alpha = 0.6, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  theme_minimal() +
  labs(title = 'Global Aging Trends | Year: {frame_time}', 
       subtitle = 'Movement shows decreasing youth and increasing aged population',
       x = 'Aged Population (%)', 
       y = 'Young Population (%)',
       caption = "Source: Global Population Dataset") +
  transition_time(Year) +       
  ease_aes('cubic-in-out') +
  shadow_wake(wake_length = 0.1, alpha = FALSE)

The animated bubble plot provides a clear visualization of the shifting demographic structures across various countries over several decades. By observing the movement of the bubbles, we can identify several key trends.

the most significant trend is the general movement from the top-left to the bottom-right of the graph. This trajectory represents a simultaneous decrease in the percentage of the young population and an increase in the proportion of the elderly. In my understanding, this is a visual representation of global population aging, driven by falling birth rates and improved medical care which extends life expectancy.

4.4 Animated Data Visualisation: plotly

gg <- ggplot(globalPop, 
       aes(x = Old, 
           y = Young, 
           size = Population, 
           colour = Country)) +
  geom_point(aes(size = Population,
                 frame = Year),
             alpha = 0.7, 
             show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  labs(x = '% Aged', 
       y = '% Young')

ggplotly(gg)

the animation highlights the disparity between continents. While European and some Asian nations (represented by specific colors) move rapidly towards the “aged” corner, many developing nations remain in the “young” zone for a longer duration.

4.4.2 Building an animated bubble plot: plot_ly() method

bp <- globalPop %>%
  plot_ly(x = ~Old, 
          y = ~Young, 
          size = ~Population, 
          color = ~Continent,
          sizes = c(2, 100),
          frame = ~Year, 
          text = ~Country, 
          hoverinfo = "text",
          type = 'scatter',
          mode = 'markers'
          ) %>%
  layout(showlegend = FALSE)
bp

In summary, this dynamic chart is far more effective than a static one because it captures the speed of change. It allows us to conclude that while aging is a universal phenomenon, the pace varies significantly between different economic regions, which is crucial for future policy planning.